package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowCandidateService;
import org.apache.commons.collections4.CollectionUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.Execution;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
　　* @description: 流程发起候选服务实现
　　* @author shenrm
　　* @date 2021/12/6 21:06
　　*/
@Service("flowApplyUserService")
public class FlowApplyUserServiceImpl implements FlowCandidateService {

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public String getNgrUser(String processInstanceId, String userCode) {
        List<Execution> executionList = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list();
        if (CollectionUtils.isNotEmpty(executionList)) {
            String executionId = executionList.get(0).getId();
            Object initiatorObj = runtimeService.getVariable(executionId, "assigned_apply_initiator");
            if (Objects.nonNull(initiatorObj) && (initiatorObj instanceof String)) {
                //获取流程发起人
                String initiator = String.valueOf(initiatorObj);
                return initiator;
            }
        }
        return null;
    }
}
